package com.my.lucky.config;

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;
import com.my.lucky.ui.MainWindow;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.swing.*;
import java.awt.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

//@Configuration
public class LogConfig {

    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");

    @Autowired
    private MainWindow mainWindow;

    @PostConstruct
    public void init() {
        // 获取LoggerContext
        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();

        // 获取根日志记录器
        Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);

        // 创建并添加自定义Appender
        SwingAppender swingAppender = new SwingAppender();
        swingAppender.setContext(loggerContext);
        swingAppender.start();
        rootLogger.addAppender(swingAppender);
    }

    // 自定义Appender，将日志输出到Swing窗口
    private class SwingAppender extends AppenderBase<ILoggingEvent> {
        @Override
        protected void append(ILoggingEvent event) {
            // 格式化日志消息
            String timestamp = LocalDateTime.now().format(DATE_FORMATTER);
            String level = event.getLevel().toString();
            String loggerName = event.getLoggerName();
            // 获取简短的类名
            String shortLoggerName = loggerName.substring(loggerName.lastIndexOf('.') + 1);
            final String logMessage = String.format("[%s] [%s] [%s] - %s",
                    timestamp, level, shortLoggerName, event.getFormattedMessage());

            // 确保在EDT线程中更新UI
            if (SwingUtilities.isEventDispatchThread()) {
                appendLog(logMessage, event.getLevel().toString());
            } else {
                SwingUtilities.invokeLater(() -> appendLog(logMessage, event.getLevel().toString()));
            }
        }

        private void appendLog(String message, String level) {
            JTextArea logArea = mainWindow.getLogArea();
            if (logArea != null) {
                // 根据日志级别选择颜色
                Color color;
                switch (level) {
                    case "ERROR":
                        color = new Color(255, 0, 0); // 红色
                        break;
                    case "WARN":
                        color = new Color(255, 165, 0); // 橙色
                        break;
                    case "INFO":
                        color = new Color(0, 0, 0); // 黑色
                        break;
                    case "DEBUG":
                        color = new Color(128, 128, 128); // 灰色
                        break;
                    default:
                        color = new Color(0, 0, 0); // 默认黑色
                        break;
                }

                logArea.setForeground(color);
                logArea.append(message + "\n");
                // 自动滚动到底部
                logArea.setCaretPosition(logArea.getDocument().getLength());
            }
        }
    }
} 